package cn.wizzer.app.web.modules.controllers.platform.sys;


import cn.wizzer.app.sys.modules.models.SysBankLib;
import cn.wizzer.app.sys.modules.services.SysBankLibService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/sys/bankLib")
public class SysBankLibController {
    @Inject
    @Reference
    private SysBankLibService bankLibService;

    @At("/")
    @Ok("beetl:/platform/sys/bankLib/index.html")
    @RequiresPermissions("sys.finance.bankLib.list")
    public void index() {
    }

    /**
     * 银行列表(模糊查询)
     *
     * @param keyWord
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.finance.bankLib.list")
    public Object list(@Param("keyWord") String keyWord) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(keyWord)) {
            cnd.and("country", "like", "%" + keyWord + "%").or("name", "like", "%" + keyWord + "%");
        }
        return Result.success().addData(bankLibService.query(cnd));
    }

    /**
     * 银行列表(树形数据 级联选择)
     *
     * @return
     */
    @At
    @Ok("json")
    @RequiresPermissions("sys.finance.bankLib.list")
    public Object treeList() {
        return Result.success(bankLibService.list());
    }


    /**
     * 查询银行列表（分页）
     *
     * @param searchKeyword String 搜索框输入的内容
     * @param searchDate    String[] 时间数组
     * @param pageNumber    int
     * @param pageSize      int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.finance.bankLib.list")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(bankLibService.listPage(pageNumber, pageSize, searchKeyword, searchDate));
    }


    /**
     * 添加银行
     *
     * @param bank
     * @return
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("sys.finance.bankLib.add")
    @SLog(tag = "添加银行", msg = "Bank名:${args[0].name}")
    public Object add(@Param("..") SysBankLib bank) {
        bankLibService.clearCache();
        bankLibService.add(bank);
        return Result.success();
    }


    /**
     * 根据id查询银行信息
     *
     * @param id
     * @return
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(@Param("id") String id) {
        return Result.success(bankLibService.fetch(id));
    }

    /**
     * 修改银行信息
     *
     * @param bank
     * @return
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("sys.finance.bankLib.edit")
    @SLog(tag = "修改银行", msg = "银行卡:${args[1].getAttribute('name')}")
    public Object editDo(@Param("..") SysBankLib bank) {
        bankLibService.clearCache();
        bankLibService.edit(bank);
        return Result.success();
    }

    /**
     * 根据id启用银行
     *
     * @param id
     * @return
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.bankLib.edit")
    @SLog(tag = "启用银行", msg = "银行名:${args[1].getAttribute('name')}")
    public Object enable(String id) {
        bankLibService.clearCache();
        bankLibService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 根据id禁用银行
     *
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.bankLib.edit")
    @SLog(tag = "禁用银行", msg = "银行名:${args[1].getAttribute('name')}")
    public Object disable(String id) {
        bankLibService.clearCache();
        bankLibService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 根据id删除银行
     *
     * @param id
     * @return
     */
    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.bankLib.delete")
    public Object delete(String id) {
        bankLibService.clearCache();
        bankLibService.delete(id);
        return Result.success();
    }
}
